package com.sicnu.cheer.leaugebar.activity.profileset;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.sicnu.cheer.generalmodule.config.AppManager;
import com.sicnu.cheer.generalmodule.util.SharedPreferencesUtil;
import com.sicnu.cheer.generalmodule.wheelview.OnWheelChangedListener;
import com.sicnu.cheer.generalmodule.wheelview.WheelView;
import com.sicnu.cheer.generalmodule.wheelview.adapters.ArrayWheelAdapter;
import com.sicnu.cheer.profileset.R;
import com.sicnu.cheer.profileset.config.Constant;
import com.sicnu.cheer.profileset.utils.ImageUtil;

/**
 * Created by cheer on 2017/2/10.
 * 个人资料导航页，用户第一次登录时对个人资料的设置之家乡设置
 */
public class HometownSetActivity extends WheelBaseActivity implements View.OnClickListener, OnWheelChangedListener {

    private HometownSetActivity mThis;
    private WheelView mViewProvince;
    private WheelView mViewCity;
    private WheelView mViewDistrict;
    private ImageView sexIV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hometown_set);
        mThis = this;
        AppManager.getAppManager().addActivity(mThis);
        initView();

        setUpViews();
        setUpListener();
        setUpData();
    }


    private void initView() {

        //展示已选择的性别
        sexIV = ((ImageView) findViewById(R.id.sex_iv));
        ImageUtil.setSexIV(mThis,sexIV);

        findViewById(R.id.forward_step_tv).setOnClickListener(this);
        findViewById(R.id.next_step_tv).setOnClickListener(this);
    }
    private void setUpViews() {
        mViewProvince = (WheelView) findViewById(R.id.id_province);
        mViewCity = (WheelView) findViewById(R.id.id_city);
        mViewDistrict = (WheelView) findViewById(R.id.id_district);

    }

    private void setUpListener() {
        // 添加change事件
        mViewProvince.addChangingListener(this);
        // 添加change事件
        mViewCity.addChangingListener(this);
        // 添加change事件
        mViewDistrict.addChangingListener(this);
    }

    private void setUpData() {
        initProvinceDatas();
        mViewProvince.setViewAdapter(new ArrayWheelAdapter<>(mThis, mProvinceDatas));
        // 设置可见条目数量
        mViewProvince.setVisibleItems(7);
        mViewCity.setVisibleItems(7);
        mViewDistrict.setVisibleItems(7);
        updateCities();
        updateAreas();
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (wheel == mViewProvince) {
            updateCities();
        } else if (wheel == mViewCity) {
            updateAreas();
        } else if (wheel == mViewDistrict) {
            mCurrentDistrictName = mDistrictDatasMap.get(mCurrentCityName)[newValue];
            mCurrentZipCode = mZipcodeDatasMap.get(mCurrentDistrictName);
        }
    }

    /**
     * 根据当前的市，更新区WheelView的信息
     */
    private void updateAreas() {
        int pCurrent = mViewCity.getCurrentItem();
        mCurrentCityName = mCitisDatasMap.get(mCurrentProviceName)[pCurrent];
        String[] areas = mDistrictDatasMap.get(mCurrentCityName);

        if (areas == null) {
            areas = new String[] { "" };
        }
        mViewDistrict.setViewAdapter(new ArrayWheelAdapter<String>(mThis, areas));
        mViewDistrict.setCurrentItem(0);
    }

    /**
     * 根据当前的省，更新市WheelView的信息
     */
    private void updateCities() {
        int pCurrent = mViewProvince.getCurrentItem();
        mCurrentProviceName = mProvinceDatas[pCurrent];
        String[] cities = mCitisDatasMap.get(mCurrentProviceName);
        if (cities == null) {
            cities = new String[] { "" };
        }
        mViewCity.setViewAdapter(new ArrayWheelAdapter<String>(mThis, cities));
        mViewCity.setCurrentItem(0);
        updateAreas();
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.forward_step_tv){
            finish();
        }else if (v.getId()==R.id.next_step_tv){
            //保存家乡到本地
            SharedPreferencesUtil.saveUserItem(mThis, Constant.HOMETOWN,
                    mCurrentProviceName+"-"+mCurrentCityName+"-"+mCurrentDistrictName);

            Toast.makeText(mThis, "当前选中:"+mCurrentProviceName+","+mCurrentCityName+","
                    +mCurrentDistrictName+","+mCurrentZipCode, Toast.LENGTH_SHORT).show();

            //跳转到昵称设置界面
            Intent intent=new Intent(mThis,NicknameSetActivity.class);
            startActivity(intent);
        }
    }
}
